package com.itheima.store.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.itheima.store.dao.BookDao;
import com.itheima.store.dao.UserDao;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.User;
import com.itheima.store.domain.Book;
import com.itheima.store.service.BookService;
import com.itheima.store.utils.BeanFactory;
/**
 * 商品Service的实现类
 * @author admin
 *
 */
public class BookServiceImpl implements BookService {

	@Override
	public List<Book> findByHot()throws SQLException  {
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		return bookDao.findByHot();
	}

	@Override
	public List<Book> findByNew()throws SQLException  {
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		return bookDao.findByNew();
	}

	@Override
	public PageBean<Book> findByPageBook_category_id(String book_category_id, Integer currPage) throws SQLException {
		PageBean<Book> pageBean = new PageBean<Book>();
		// 设置参数：
		// 设置当前页数：
		pageBean.setCurrPage(currPage);
		// 设置每页显示的记录数:
		Integer pageSize = 12;
		pageBean.setPageSize(pageSize);
		// 设置总记录数:
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		Integer totalCount = bookDao.findCountByBook_category_id(book_category_id);
		pageBean.setTotalCount(totalCount);
		// 设置总页数:
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());
		// 设置每页显示的数据的集合:
		int begin = (currPage - 1) * pageSize;
		List<Book> list = bookDao.findPageByBook_category_id(book_category_id,begin,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public Book findByBook_id(String book_id) throws SQLException {
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		return bookDao.findByBook_id(book_id);
	}

	@Override
	public PageBean<Book> findByPage(Integer currPage) throws SQLException {
		PageBean<Book> pageBean = new PageBean<Book>();
		//设置参数
		Integer pageSize = new Integer(10);
		pageBean.setPageSize(pageSize);//设置页面的大小(个数）
		pageBean.setCurrPage(currPage);//设置当前页数
		BookDao bookDao = (BookDao )BeanFactory.getBean("bookDao");
		Integer countAll = bookDao.findcountAll();
		pageBean.setTotalCount(countAll);
		//设置总页数
		double tc = countAll;
		Double num = Math.ceil(tc/pageSize);
		pageBean.setTotalPage(num.intValue());
		//设置每页显示的数据的集合
		int begin = (currPage-1)*pageSize;
		List<Book> list =bookDao.finByPage(begin,pageSize);
		pageBean.setList(list);
		
		return pageBean;
		
	}

	@Override
	public PageBean<Book> findBookAllPageBean(int currPage,String search) throws SQLException {
		// TODO Auto-generated method stub
		PageBean<Book> pageBean = new PageBean<Book>();
		pageBean.setCurrPage(currPage);
		//1.设置每页的大小
	   int pageSize = 10;
	   BookDao bookDao = (BookDao)BeanFactory.getBean("bookDao");
	   //2.获取总条数 totalCount
	   Integer totalCount = null;
	   if(!"".equals(search) && search != null) {
		    totalCount = bookDao.findAllByPage(search);
	   }else {
		   totalCount = bookDao.findAllByPage("9");
	   }
	   
		double tc  = totalCount;
		Double num =Math.ceil(tc /pageSize);
		pageBean.setTotalPage(num.intValue());
		//设置每页显示的数据的集合
		int begin = (currPage - 1)*pageSize;
	    List<Book> list  = bookDao.findAllByPage(begin,pageSize,search);
	    pageBean.setList(list);
	    
		return pageBean;
	}
	
	@Override
	public void save(Book book) throws SQLException {
		// TODO Auto-generated method stub
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		bookDao.save(book);
	}

	@Override
	public void update(Book book) throws SQLException {
		// TODO Auto-generated method stub
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		bookDao.update(book);
	}

	@Override
	public void updateEdit(Book book) throws SQLException {
		// TODO Auto-generated method stub
		((BookDao) BeanFactory.getBean("bookDao")).updateEdit(book);
	}

	
	@Override
	public List<Book> findPushDown(Integer isunder) throws SQLException {
		// TODO Auto-generated method stub
		BookDao bookDao = (BookDao)BeanFactory.getBean("bookDao");
		return bookDao.finPushDown(isunder);
	}

	@Override
	public void delete(String book_id) throws SQLException {
		// TODO Auto-generated method stub
		BookDao bookDao = (BookDao)BeanFactory.getBean("bookDao");
		bookDao.delete(book_id);
	}

	@Override
	public PageBean<Book> findByPageBookName(String book_name, Integer currPage) throws SQLException {
		PageBean<Book> pageBean = new PageBean<Book>();
		// 设置参数：
		// 设置当前页数：
		pageBean.setCurrPage(currPage);
		// 设置每页显示的记录数:
		Integer pageSize = 12;
		pageBean.setPageSize(pageSize);
		// 设置总记录数:
		BookDao bookDao = (BookDao) BeanFactory.getBean("bookDao");
		Integer totalCount = bookDao.findCountByBookName(book_name);
		pageBean.setTotalCount(totalCount);
		// 设置总页数:
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());
		// 设置每页显示的数据的集合:
		int begin = (currPage - 1) * pageSize;
		List<Book> list = bookDao.findPageByBookName(book_name,begin,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	
}
